package com.bocom.vo;

import com.bocom.enums.ApiStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @author gusuchen
 * Created in 2018-05-18 16:14
 * Description: 封装全局统一的返回格式
 */
@Getter
@Setter
@ToString
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ApiResponseVO<T> {

    @JsonView(ApiResponseSimpleView.class)
    private String code;

    @JsonView(ApiResponseSimpleView.class)
    private String msg;

    @JsonView(ApiResponseResultView.class)
    private T data;


    private ApiResponseVO() {
        this.code = ApiStatus.SUCCESS.getCode();
        this.msg = ApiStatus.SUCCESS.getMessage();
    }

    private ApiResponseVO(T data) {
        this.code = ApiStatus.SUCCESS.getCode();
        this.msg = ApiStatus.SUCCESS.getMessage();
        this.data = data;
    }

    private ApiResponseVO(String code, String message) {
        this.code = code;
        this.msg = message;
    }

    public static <T> ApiResponseVO<T> ofSuccess() {
        return new ApiResponseVO<>();
    }

    public static <T> ApiResponseVO<T> ofError() {
        return ofStatus(ApiStatus.ERROR);
    }

    public static <T> ApiResponseVO<T> ofResult(T data) {
        return new ApiResponseVO<>(data);
    }

    public static <T> ApiResponseVO<T> ofMessage(String code, String message) {
        return new ApiResponseVO<>(code, message);
    }

    public static <T> ApiResponseVO<T> ofStatus(ApiStatus status) {
        return new ApiResponseVO<>(status.getCode(), status.getMessage());
    }

    public interface ApiResponseSimpleView {
    }

    public interface ApiResponseResultView extends ApiResponseSimpleView {
    }

}
